//购物车
import { observable,action,autorun,computed} from 'mobx';

class CartGoods{
    @observable allDatas = [];
    @observable CartCount = 0;
    // autorun(() =>{
    //     let count = 0;
    //     this.allDatas.map(i => {
    //         count += i.count
    //     });
    //     this.CartCount = count;
    // })
    @computed get getCartCount(){
        let count = 0;
        this.allDatas.map(i => {
            count += i.count;
        });
        return count;
    }

    @action
    AddToCart(item){
        let f = this.allDatas.filter(f => f.id == item.id);//[]
        let count = 0;
        if(f.length > 0){
            this.allDatas.remove(f[0]);
            count = f[0].count;
        }
        this.allDatas.push({
            id:item.id,
            name:item.name,
            price:item.price,
            count:count+1
        });
    }
}

const cartGoods = new CartGoods();

export {cartGoods};